//
//  SEPatientListCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

enum SEPatientListCellBtnActionType {
    case edit
    case chooseDefault
}

protocol SEPatientListCellDelegate: NSObjectProtocol {
    func patientListCellBtnAction(actionType: SEPatientListCellBtnActionType, indexPath: IndexPath)
}

class SEPatientListCell: UITableViewCell {

    static let className = "SEPatientListCell"
    static let reuseId = className

    @IBOutlet weak var nameLabelLeading: NSLayoutConstraint!
    @IBOutlet weak var bgView: UIView! {
        didSet {
            bgView.layer.cornerRadius = 4
            bgView.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var defaultLabel: UILabel! {
        didSet {
            defaultLabel.layer.cornerRadius = 2
            defaultLabel.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var sexLabel: UILabel!
    @IBOutlet weak var ageLabel: UILabel!
    @IBOutlet weak var defaultBtn: UIButton!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBAction func defaultBtnAction(_ sender: UIButton) {
        guard sender.isSelected == false else { return }
        delegate?.patientListCellBtnAction(actionType: .chooseDefault, indexPath: indexPath)
    }
    @IBAction func editBtnAction(_ sender: UIButton) {
        delegate?.patientListCellBtnAction(actionType: .edit, indexPath: indexPath)
    }

    weak var delegate: SEPatientListCellDelegate?
    var indexPath = IndexPath(row: 0, section: 0)

    func updateCell(viewModel: SEPatientDetailViewModel, indexPath: IndexPath) {
        self.indexPath = indexPath
        nameLabel.text = viewModel.model.name
        sexLabel.text = viewModel.sexStr
        ageLabel.text = viewModel.model.age
        phoneLabel.text = viewModel.model.phone
        defaultBtn.isSelected = viewModel.model.defaultPatient == 1 ? true : false
        defaultLabel.isHidden = viewModel.model.defaultPatient == 1 ? false : true
        nameLabelLeading.constant = viewModel.model.defaultPatient == 1 ? 45 : 15
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
